close all;
clear;clc;

Puter_Name = getenv('computername');
if strcmp(Puter_Name, 'CALCENTER2')
    Puter_Name_Pre = 'D:\vc\mail.ustc.edu.cn\Rb87BEC - Files\';
elseif strcmp(Puter_Name, 'VICTORCHENGPC')
    Puter_Name_Pre = 'X:\Work\mail.ustc.edu.cn\Rb87BEC - Files\';
end
Row_Start = 202;
mz_simed = -0.25;
if mz_simed < 0
    shift = 2;
else
    shift = 0;
end
UpDown_Shift = 3;
load(sprintf("%s\\Rb87Data\\3DSOCbandData\\190827\\ky_detuning_pair,mz_sim=%.3f.mat", Puter_Name_Pre, mz_simed))

load(sprintf("%s\\Rb87Data\\3DSOCbandData\\190827\\3x3x3;4x41x41x41;V_xz=1.770E_r,V_y=-3.540E_r,Oxy=1.022E_r,Ozy=-1.022E_r,T=1.000e-07K,n=3.000e+18m-3del_psi=1.000pi,mz=%.3fErno_states_v1.mat", Puter_Name_Pre, mz_simed))

if Row_Start == 119
    ky_detuning_pair(9:21,:) = [];
end
if Row_Start == 98 || 202
    ky_detuning_pair = flip(ky_detuning_pair);
end
detuning_measure_list = ky_detuning_pair(:, 2);
qy_measure_list = ky_detuning_pair(:, 1);
% detuning_measure_list = [0.03 0.025 detuning_measure_list];
% qy_measure_list = [0.05 0 qy_measure_list];

Z_Spacing = 1.3;
clims = [-0.5, 0.5];

if strcmp(Puter_Name, 'CALCENTER2')
    load("D:\vc\Rb87Python\colormap_symmetric.mat");
elseif strcmp(Puter_Name, 'VICTORCHENGPC')
%     load("X:\work\Rb87Python\colormap_thinwhite.mat");
    load("X:\work\Rb87Python\colormap_symmetric.mat");
end

if mz_simed < 0
    M_Center_Switch = 1;
else
    M_Center_Switch = 0;
end
if M_Center_Switch == 1
    Center_Point = 'M';
else
    Center_Point = 'Gamma';
end

% Data_Excel = "D:\Rb87files\3DSOC_spin_texture\spin_texture.xlsx";
% % Data_Excel = "spin_texture.xlsx";
% Select_Matrix = readcell(Data_Excel, 'Sheet', 'Meta');
% Select_Row_List = 77:98;


figure(1)
z_ticks = [];
z_label = {};
for detuning_measure_index = 1:length(detuning_measure_list)
    detuning_measure = detuning_measure_list(detuning_measure_index);
    qy_measure = qy_measure_list(detuning_measure_index);
    qy_measure_ori = qy_measure_list(detuning_measure_index);
    if Row_Start == 98 || 202
        if qy_measure < 0.5 && qy_measure ~= 0
            continue
        end
%         if mod(detuning_measure_index, 2) == 0
%             continue
%         end
        if qy_measure == 0
            qy_measure = 0.5-0.05*UpDown_Shift;
        end
    end
    Row = Row_Start+detuning_measure_index-1;
    SP_Ave = squeeze(Spin_Texture(:, abs(ky_grid - qy_measure)<1e-10, :));
    FBZ = size(SP_Ave);
    if FBZ(1) == 1
        continue
    end
    if M_Center_Switch == 1
        SP_Ave = [SP_Ave(ceil(FBZ(1)/2):end-1,:);SP_Ave(1:ceil(FBZ(1)/2),:)];
        SP_Ave = [SP_Ave(:, ceil(FBZ(2)/2):end-1),SP_Ave(:,1:ceil(FBZ(2)/2))];
    end
    ku_list = linspace(-1, 1, FBZ(1));
    kv_list = linspace(-1, 1, FBZ(2));
    [Y, X] = meshgrid(kv_list, ku_list);
    Z = Z_Spacing*(zeros(size(Y))+qy_measure);
    z_ticks(length(z_ticks)+1) = Z(1,1);
%     z_label{length(z_label)+1}=sprintf("mz_PWM=%.3fEr, ky=%.3fpi", detuning_measure/2, qy_measure);
    z_label{length(z_label)+1}=sprintf("%.3fpi", qy_measure_ori);
    hold on;
    h = surf(X, Y, Z, SP_Ave);
    set(h,'edgecolor','none','lineStyle','none');
    material dull
    axis equal
    colormap(cmap);
    caxis(clims)
%     text(1.5,1.5,Z_Spacing*ky_simed,sprintf("detuning=%.3fEr, ky=%.3fpi",detuning, ky_simed/pi));
    pause(0.01)
end

for detuning_measure_index = 1:length(detuning_measure_list)
    detuning_measure = detuning_measure_list(detuning_measure_index);
    qy_measure = -qy_measure_list(detuning_measure_index)+shift;
    qy_measure_ori = -qy_measure_list(detuning_measure_index)+shift;
    if Row_Start == 98 || 202
        if qy_measure > 1.5 && qy_measure ~= 2
            continue
        end
        if qy_measure == 2
            qy_measure = 1.5+0.05*UpDown_Shift;
        end
%         if mod(detuning_measure_index, 2) == 0
%             continue
%         end
    end
    if qy_measure_list(detuning_measure_index) == 100
        continue
    end
%     if qy_measure < -0.5
%         continue
%     end
    Row = Row_Start+detuning_measure_index-1;
    SP_Ave = squeeze(Spin_Texture(:, abs(ky_grid - qy_measure + 2)<1e-10, :));
    FBZ = size(SP_Ave);
    if FBZ(1) == 1
        continue
    end
    if M_Center_Switch == 1
        SP_Ave = [SP_Ave(ceil(FBZ(1)/2):end-1,:);SP_Ave(1:ceil(FBZ(1)/2),:)];
        SP_Ave = [SP_Ave(:, ceil(FBZ(2)/2):end-1),SP_Ave(:,1:ceil(FBZ(2)/2))];
    end
    ku_list = linspace(-1, 1, FBZ(1));
    kv_list = linspace(-1, 1, FBZ(2));
    [Y, X] = meshgrid(kv_list, ku_list);
    Z = Z_Spacing*(zeros(size(Y))+qy_measure);
    z_ticks(length(z_ticks)+1) = Z(1,1);
%     z_label{length(z_label)+1}=sprintf("mz_PWM=%.3fEr, ky=%.3fpi", detuning_measure/2, qy_measure);
    z_label{length(z_label)+1}=sprintf("%.3fpi", qy_measure_ori);
    hold on;
    h = surf(X, Y, Z, SP_Ave);
    set(h,'edgecolor','none','lineStyle','none');
    material dull
    axis equal
    colormap(cmap);
    caxis(clims)
%     text(1.5,1.5,Z_Spacing*ky_simed,sprintf("detuning=%.3fEr, ky=%.3fpi",detuning, ky_simed/pi));
    pause(0.01)
end

[z_ticks,ia,~] = unique(z_ticks);
z_labels = z_label(ia);
zticks(z_ticks);
zticklabels(z_labels)

axis tight
view(30,4);

z_ticks = [0.5-0.05*UpDown_Shift 0.5:0.1:1.5 1.5+0.05*UpDown_Shift] * Z_Spacing;
te = [0 0.5:0.1:1.5 2]-1;
zticks(z_ticks);
zticklabel=strings(1,length(zticks));
for i=1:length(zticks)
    zticklabel(i)=sprintf('$%0.1f$',te(i));
end
zticklabels(zticklabel)
title('Theory');
set(gca,'PlotBoxAspectRatioMode','Auto',...
'DataAspectRatioMode','Auto',...
'YTickLabel',{'$-1$' '$0$' '$1$'},...
'XTickLabel',{'$1$' '$0$' ''},...
'Clim',[-0.5 0.5],...
'FontName','Times New Roman',...
'FontSize',35,...
'LineWidth',3)
xlh = xlabel('$q_{u}/\pi$','FontName','Times New Roman');%,'Position',[1.2,0,0.8])
ylh = ylabel('$q_{v}/\pi$','FontName','Times New Roman');%,'Position',[2,1.2,0.8])
zlh = zlabel('$q_{z}/\pi$','FontName','Times New Roman');

xlh.Position = [0.25, -1, 0.31];
ylh.Position = [1, -0.25, 0.31];
xlh.Interpreter = 'latex';
ylh.Interpreter = 'latex';
zlh.Interpreter = 'latex';
set(gcf,'position',[-1000.6667  -100.3333  624*1.7  800*1.7]);
set(gca,'OuterPosition',[0.05 0.05 0.8 0.8])

figh = gcf;
ax = gca;
ax.XTickMode = 'manual';
ax.YTickMode = 'manual';
ax.ZTickMode = 'manual';
ax.XLimMode = 'manual';
ax.YLimMode = 'manual';
ax.ZLimMode = 'manual';
po = get(gca,'Position');
ax = gca;
ax.TickLabelInterpreter = 'latex';
ax.ZAxis.Visible = 'off';
set(gca, 'Position', po);
set(gcf,'Units','Centimeters');
figh.PaperPosition = [0,0,figh.Position(3:4)];
figh.PaperSize = figh.Position(3:4);

% set(gcf,'Units','Centimeters',...
%    'PaperPosition',[14.0075 0.6345 22.7850 22.8610],'PaperSize',[50.8,24.13]);
%����ͼƬ
set(gcf,'Renderer','Painters');
print('C:\Users\cheng\Desktop\StackThe05_4','-dpdf')%,'-r0')

close all

if strcmp(Puter_Name, 'CALCENTER2')
    savefig(gcf, ['D:\Rb87figures\3DSOC_spin_texture\figures\Stack_New\Stack' 'V0=-1.770,Vy=-3.540,Oxy=1.022,mz_simed=' num2str(mz_simed) Center_Point '_Center' 'Experiment_Stack_rowstart' num2str(Row_Start) '.fig'], 'compact')
    saveas(gcf, ['D:\Rb87figures\3DSOC_spin_texture\figures\Stack_New\Stack' 'V0=-1.770,Vy=-3.540,Oxy=1.022,mz_simed=' num2str(mz_simed) Center_Point '_Center' 'Experiment_Stack_rowstart' num2str(Row_Start) '.png'])
end
close all;